package com.health.auth.controller;

import com.health.auth.form.LoginBody;
import com.health.auth.service.DoctorService;
import com.health.common.core.domain.R;
import com.health.common.core.utils.JwtUtils;
import com.health.common.core.utils.StringUtils;
import com.health.common.security.auth.AuthUtil;
import com.health.common.security.service.TokenService;
import com.health.common.security.utils.SecurityUtils;
import com.health.personal.api.domain.dto.TDoctorDto;
import com.health.personal.api.model.JwtResponse;
import com.health.personal.api.model.LoginDoctor;
import com.health.personal.api.model.po.TDoctorPo;
import com.health.system.api.model.LoginUser;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

/**
 * @author : Zzs
 * @description :
 * @createDate : 2024/12/12 下午10:36
 */
@RestController
@RequestMapping("/auth")
public class DoctorController{

    @Autowired
    private DoctorService service;
    @Autowired
    private TokenService tokenService;
    /**
     * 医生登录
     * @param tDoctorDto
     * @return
     */
    @PostMapping("/user/login")
    public R<?> login(@RequestBody TDoctorDto tDoctorDto)
    {
        // 用户登录
        LoginDoctor tDoctorPo = service.login(tDoctorDto.getEmail(),tDoctorDto.getPassword());
        // 获取登录token
        return R.ok(tokenService.info(tDoctorPo));
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/user/info")
    public R<?> userInfo(HttpServletRequest request){
        LoginDoctor loginUser = service.getLoginDoctor(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            service.DockerToken(loginUser);

            return R.ok(loginUser);
        }
        return R.ok(loginUser);
    }

    /**
     * 登出
     * @return
     */
    @PostMapping("/user/logout")
    public R logout(HttpServletRequest request){
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
        }
        return R.ok();
    }












}

